<script setup>
import router from "@/router";
import {nextTick, onMounted, ref} from "vue";
import {ElMessage} from "element-plus";
//6.2
import { DrawScene, DrawControl,OperateEventType } from 'bmap-draw';
import qs from "qs";
import axios from "axios";

//定义变量控制弹窗是否出现
const dialogVisible = ref(false);
//定义新增围栏的方法
const saveGeofence = () =>{
  if (!geofenceForm.value.name || geofenceForm.value.name.trim().length==0){
    ElMessage.error('围栏名称不能为空');
    return;
  }
  console.log(geofenceForm.value);
  //geofenceForm的position也是一个对象,所以需要先转换position,再转换geofenceForm,才能给后端发送请求
  geofenceForm.value.position = JSON.stringify(geofenceForm.value.position);
  let data = qs.stringify(geofenceForm.value);
  //给后端发送新增围栏的请求
  axios.post(BASE_URL+'/v1/geofence/save',data)
      .then((response)=>{
        if (response.data.code == 2000){
          ElMessage.success('保存成功');
          //关闭弹窗
          dialogVisible.value = false;
          //跳转到围栏列表界面
          router.push('/geofence');
        }else{
          ElMessage.error(response.data.message);
        }
      })
};
//定义关闭弹窗的方法
const closeDialog = () =>{
  if (confirm('确认取消吗?')){
    dialogVisible.value = false;
    //清除地图上绘制的围栏遮罩类型
    map.clearOverlays();
  }
};
//定义变量保存围栏信息
const geofenceForm = ref({
  name:'',//围栏名称
  position:{}//围栏位置信息
});
//定义变量用来保存全局地图对象
var map;
//
onMounted(()=>{
  //表示在DO更新之后再执行,因为要确保上方容器地图已创建
  nextTick(()=>{
    //3.创建地图实例,不用导入
    map = new BMapGL.Map("mapContainer");
    //4.设置中心点经纬度坐标
    var point = new BMapGL.Point(116.404, 39.915);
    //5.初始化地图
    map.centerAndZoom(point, 15);
    //6.添加鼠标绘制控件
    //6.1创建绘制场景类,需要导入!此类为其他鼠标绘制的基类,参数为刚刚创建好的地图对象
    const scene = new DrawScene(map);
    //6.2创建鼠标绘制控件对象,基于上面的绘制场景,设置自己的绘制项,需要导入!
    const drawContrl = new DrawControl(scene, {
      //6.3绘制控件的配置信息
      //6.3.1对于每一个绘制项的提示,默认关闭
      enableTips: false,
      //6.3.2设置绘制控件的定位,默认在右上角
      anchor: BMAP_ANCHOR_TOP_RIGHT,
      //6.3.3设置绘制项 circle圆形 rectangle 矩形
      drawingItems: [
        'circle',
        'rectangle'
      ]
    });
    //6.5监听绘制事件,返回触发完成事件的电子围栏对象
    scene.addEventListener(OperateEventType.COMPLETE,(event)=>{
      //10用户点击对号时,显示输入围栏名称弹窗
      dialogVisible.value = true;
      //打印触发这个事件的目标对象(圆形会矩形围栏)target目标对象
      console.log(event.target);
      //6.6从触发事件对象中获取围栏对象
      let overlay = event.target.overlay;
      //6.7判断当前围栏类型是圆形还是矩形,分别处理
      //instanceof用来判断overlay对象是否为BMapGL.Circle类的实例
      if(overlay instanceof BMapGL.Circle){//左边的overlay是否为圆形
        //6.8.1设置围栏类型为圆形
        geofenceForm.value.position.type = 'circle';
        //6.8.2设置围栏半径
        geofenceForm.value.position.radius = overlay.getRadius();
        //6.8.3设置围栏圆心的经纬度
        geofenceForm.value.position.longitude = overlay.getCenter().lng;
        geofenceForm.value.position.latitude = overlay.getCenter().lat;
        console.log(geofenceForm.value);
        console.log(geofenceForm.value.position);
      }else{//是矩形
        //6.9.1定义数组用来保存四个顶点坐标值
        let recPoints = [];
        for(var i = 0;i<4;i++){
          //循环4次,依次取出围栏对象cA坐标系下的四个顶点,并以 经度-纬度 的形势存入数组中
          recPoints.push(overlay.points[i].latLng.lng+'-'+overlay.points[i].latLng.lat);
        }
        //设置围栏矩形为矩形
        geofenceForm.value.position.type = 'rectangle';
        //将矩形坐标数组对象,转为以逗号分隔的字符串
        geofenceForm.value.position.recPoints = recPoints.join(',');
        console.log(geofenceForm.value);
        console.log(geofenceForm.value.position);
      }
    });
    //6.4为地图设置绘制控件,参数为刚刚创建好的绘制控件对象
    map.addControl(drawContrl);
  })
})
</script>

<template>
  <div style="height: 6vh;background-color: #fff;padding: 10px 20px;">
    <span style="line-height: 60px;font-size: 20px;">新增围栏</span>
    <el-button type="primary" style="float: right;margin-top: 13px;"
    @click="router.push('/geofence')">返回</el-button>
  </div>

  <!-- 2.准备一个用来装地图的容器 -->
  <div id="mapContainer" style="height: 84vh;width: 100%">

  </div>

  <!-- 新增电子围栏弹窗 -->
  <el-dialog title="围栏信息" v-model="dialogVisible" :before-close="closeDialog">
    <el-form label-width="80px">
      <el-form-item label="围栏名称">
        <el-input placeholder="请输入围栏名称" v-model="geofenceForm.name"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="saveGeofence">确定</el-button>
    </template>
  </el-dialog>
</template>

<style scoped>

</style>